﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Yglab.Models;
using Yglab.MyLib;

namespace Yglab.Controllers
{
    [AdminLogControlFilter]
    public class AdminController : Controller
    {
        private YgContext conn = new YgContext();

        [HttpPost]
        public ActionResult AddJournal(Journal newJournal)
        {
            conn.Journals.Add(newJournal);
            conn.SaveChanges();
            return RedirectToAction("Journals");
        }
        

        public ActionResult Journals()
        {
            List<Journal> journals = conn.Journals.Include("Editor").ToList();
            List<Gui_Journal> gui_journals = new List<Gui_Journal>();

            foreach(Journal j in journals)
            {
                Gui_Journal new_gui_journal = new Gui_Journal();
                new_gui_journal.Journal = j;

                if (j.Editor != null)
                {
                    new_gui_journal.State = 1;
                }
                else
                {
                    List<Invitation> invitations = conn.Invitations.Include("Journal").Include("InvitedUser").Where(i => i.Journal.Id == j.Id).ToList();
                    if (invitations.Count != 1)
                    {
                        new_gui_journal.State = 3;
                    }
                    else
                    {
                        new_gui_journal.State = 2;
                        new_gui_journal.Invitation = invitations.ElementAt(0);
                    }
                }

                gui_journals.Add(new_gui_journal);
            }
            return View(gui_journals);
        }

        public ActionResult InviteEditor(int journalId, string invitedEmail)
        {
            List<User> invitedEditors = conn.Users.Where(u => u.Email.Equals(invitedEmail)).ToList();
            Journal journal = conn.Journals.Find(journalId);

            if (invitedEditors.Count != 1)
            {
                conn.Invitations.Add(new Invitation { Journal = journal, InvitationType = 1, InvitedUserEmail = invitedEmail, UserType = 2, State = 3 } );
            }
            else
            {
                Invitation addedInv = conn.Invitations.Add(new Invitation { Journal = journal, InvitationType = 1, UserType = 1, InvitedUser = invitedEditors.ElementAt(0), State = 3 } );
                conn.Notifications.Add(new Notification { Invitation = addedInv, Type = 1, User = invitedEditors.ElementAt(0) });
            }
            conn.SaveChanges();
            return RedirectToAction("Journals");
        }

        public ActionResult SubjectsAndSciences()
        {
            Gui_SubjectsSciences ss = new Gui_SubjectsSciences { 
                Sciences=conn.Sciences.OrderBy(s=>s.Name).ToList(), 
                Subjects = conn.Subjects.OrderBy(s=>s.Name).ToList()
            };
            return View(ss);
        }

        public ActionResult AddSubject(Subject subject)
        {
            conn.Subjects.Add(new Subject { Name = subject.Name });
            conn.SaveChanges();
            return RedirectToAction("SubjectsAndSciences");
        }

        public ActionResult AddScience(Science science)
        {
            conn.Sciences.Add(new Science { Name = science.Name });
            conn.SaveChanges();
            return RedirectToAction("SubjectsAndSciences");
        }
        public ActionResult DeleteSubject(int Id)
        {
            Subject s = conn.Subjects.Find(Id);
            conn.Subjects.Remove(s);
            conn.SaveChanges();
            return RedirectToAction("SubjectsAndSciences");
        }
        public ActionResult DeleteScience(int Id)
        {
            Science s = conn.Sciences.Find(Id);
            conn.Sciences.Remove(s);
            conn.SaveChanges();
            return RedirectToAction("SubjectsAndSciences");
        }

    }

    public class AdminLogControlFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            User user = Yglab.MyLib.MySingletonSession.Current.CurrentUser;
            if (user != null && user.isAdmin()) { }
            else
            {
                RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("action", "Index");
                redirectTargetDictionary.Add("controller", "Home");

                filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }
        }
    }

}
